<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="cm_westwood">
    
    <title>
        
            第1周 |
        
        西木同学的代码训练 2022
    </title>
    
<link rel="stylesheet" href="/code_training_2022/css/style.css">

    <link rel="shortcut icon" href="">
    
<link rel="stylesheet" href="/code_training_2022/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"cm_westwood.gitee.io","root":"/code_training_2022/","language":"en","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#0066CC","avatar":"","favicon":"","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"https://xpoet.cn/images/bg.svg","description":"Keep writing and Keep loving."},"scroll":{"progress_bar":{"enable":true},"percent":{"enable":true}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":true},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"};
  </script>
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                西木同学的代码训练 2022
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/"
                            >
                                HOME
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/archives"
                            >
                                ARCHIVES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/tags"
                            >
                                TAGS
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/">HOME</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/archives">ARCHIVES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/tags">TAGS</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">第1周</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">cm_westwood</span>
                        
                            <span class="author-label">Lv2</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2022-06-19 12:30:21</span>
        <span class="mobile">2022-06-19 12:30</span>
    </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/code_training_2022/tags/Python/">Python</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <p>补充 Python 边边角角的小知识，学习 Python 中的函数重载，Python 中的 <code>__init__.py</code></p>
<span id="more"></span>

<h1 id="python-magic"><a href="#python-magic" class="headerlink" title="python magic"></a>python magic</h1><p>阅读 <a class="link"   target="_blank" rel="noopener" href="https://github.com/iswbm/magic-python" >https://github.com/iswbm/magic-python<i class="fas fa-external-link-alt"></i></a> 发现了不少边边角角的 Python 知识，<a href="/code_training_2022/第1周/python-magic.html">这里列举一些好用的小方法</a>。</p>
<h1 id="Python-中的函数重载"><a href="#Python-中的函数重载" class="headerlink" title="Python 中的函数重载"></a>Python 中的函数重载</h1><p>Python 对函数的定义并没有重载这个概念，因为 Python 函数不需要限定参数类型，又可以接受可变参数，因此函数默认是有重载功能的，但是为了方便函数能够像重载函数那样书写，需要借助单分派泛函数，在 Python 中只能实现基于单个（第一个）参数的数据类型来选择具体的实现方式，官方名称 是 <code>single-dispatch</code>，可以实现第一个参数的数据类型不同，其调用的函数也就不同。<code>singledispatch</code> 是 PEP443 中引入的，如果你对此有兴趣，PEP443 应该是最好的学习文档：<a class="link"   target="_blank" rel="noopener" href="https://www.python.org/dev/peps/pep-0443/" >https://www.python.org/dev/peps/pep-0443/<i class="fas fa-external-link-alt"></i></a></p>
<p>Python 中有许许多的数据类型，比如 str，list， dict， tuple 等，不同数据类型的拼接方式各不相同，</p>
<p><strong>使用 single-dispatch 的实现方法：</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">check_type</span>(<span class="params">func</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">*args</span>):</span><br><span class="line">        arg1, arg2 = args[:<span class="number">2</span>]</span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">type</span>(arg1) != <span class="built_in">type</span>(arg2):</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&#x27;【错误】：参数类型不同，无法拼接!!&#x27;</span></span><br><span class="line">        <span class="keyword">return</span> func(*args)</span><br><span class="line">    <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@singledispatch</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">obj, new_obj</span>):</span><br><span class="line">    <span class="keyword">raise</span> TypeError</span><br><span class="line"></span><br><span class="line"><span class="meta">@add.register(<span class="params"><span class="built_in">str</span></span>)</span></span><br><span class="line"><span class="meta">@check_type</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">_</span>(<span class="params">obj, new_obj</span>):</span><br><span class="line">    obj += new_obj</span><br><span class="line">    <span class="keyword">return</span> obj</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@add.register(<span class="params"><span class="built_in">list</span></span>)</span></span><br><span class="line"><span class="meta">@check_type</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">_</span>(<span class="params">obj, new_obj</span>):</span><br><span class="line">    obj.extend(new_obj)</span><br><span class="line">    <span class="keyword">return</span> obj</span><br><span class="line"></span><br><span class="line"><span class="meta">@add.register(<span class="params"><span class="built_in">dict</span></span>)</span></span><br><span class="line"><span class="meta">@check_type</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">_</span>(<span class="params">obj, new_obj</span>):</span><br><span class="line">    obj.update(new_obj)</span><br><span class="line">    <span class="keyword">return</span> obj</span><br><span class="line"></span><br><span class="line"><span class="meta">@add.register(<span class="params"><span class="built_in">tuple</span></span>)</span></span><br><span class="line"><span class="meta">@check_type</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">_</span>(<span class="params">obj, new_obj</span>):</span><br><span class="line">    <span class="keyword">return</span> (*obj, *new_obj)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(add(<span class="string">&#x27;hello&#x27;</span>,<span class="string">&#x27;, world&#x27;</span>))</span><br><span class="line"><span class="built_in">print</span>(add([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>], [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]))</span><br><span class="line"><span class="built_in">print</span>(add(&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wangbm&#x27;</span>&#125;, &#123;<span class="string">&#x27;age&#x27;</span>:<span class="number">25</span>&#125;))</span><br><span class="line"><span class="built_in">print</span>(add((<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;huawei&#x27;</span>), (<span class="string">&#x27;vivo&#x27;</span>, <span class="string">&#x27;oppo&#x27;</span>)))</span><br><span class="line"></span><br><span class="line"><span class="comment"># list 和 字符串 无法拼接</span></span><br><span class="line"><span class="built_in">print</span>(add([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>], <span class="string">&#x27;4,5,6&#x27;</span>))</span><br></pre></td></tr></table></figure>

<p>输出结果如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">hello, world</span><br><span class="line">[1, 2, 3, 4, 5, 6]</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wangbm&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: 25&#125;</span><br><span class="line">(<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;huawei&#x27;</span>, <span class="string">&#x27;vivo&#x27;</span>, <span class="string">&#x27;oppo&#x27;</span>)</span><br><span class="line">【错误】：参数类型不同，无法拼接!!</span><br></pre></td></tr></table></figure>

<p><strong>如果不使用 singledispatch ：</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">check_type</span>(<span class="params">func</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">*args</span>):</span><br><span class="line">        arg1, arg2 = args[:<span class="number">2</span>]</span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">type</span>(arg1) != <span class="built_in">type</span>(arg2):</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&#x27;【错误】：参数类型不同，无法拼接!!&#x27;</span></span><br><span class="line">        <span class="keyword">return</span> func(*args)</span><br><span class="line">    <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"><span class="meta">@check_type</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">obj, new_obj</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(obj, <span class="built_in">str</span>) :</span><br><span class="line">        obj += new_obj</span><br><span class="line">        <span class="keyword">return</span> obj</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(obj, <span class="built_in">list</span>) :</span><br><span class="line">        obj.extend(new_obj)</span><br><span class="line">        <span class="keyword">return</span> obj</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(obj, <span class="built_in">dict</span>) :</span><br><span class="line">        obj.update(new_obj)</span><br><span class="line">        <span class="keyword">return</span> obj</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(obj, <span class="built_in">tuple</span>) :</span><br><span class="line">        <span class="keyword">return</span> (*obj, *new_obj)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(add(<span class="string">&#x27;hello&#x27;</span>,<span class="string">&#x27;, world&#x27;</span>))</span><br><span class="line"><span class="built_in">print</span>(add([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>], [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]))</span><br><span class="line"><span class="built_in">print</span>(add(&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wangbm&#x27;</span>&#125;, &#123;<span class="string">&#x27;age&#x27;</span>:<span class="number">25</span>&#125;))</span><br><span class="line"><span class="built_in">print</span>(add((<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;huawei&#x27;</span>), (<span class="string">&#x27;vivo&#x27;</span>, <span class="string">&#x27;oppo&#x27;</span>)))</span><br><span class="line"></span><br><span class="line"><span class="comment"># list 和 字符串 无法拼接</span></span><br><span class="line"><span class="built_in">print</span>(add([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>], <span class="string">&#x27;4,5,6&#x27;</span>))</span><br></pre></td></tr></table></figure>

<p>输出如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">hello, world</span><br><span class="line">[1, 2, 3, 4, 5, 6]</span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;wangbm&#x27;</span>, <span class="string">&#x27;age&#x27;</span>: 25&#125;</span><br><span class="line">(<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;huawei&#x27;</span>, <span class="string">&#x27;vivo&#x27;</span>, <span class="string">&#x27;oppo&#x27;</span>)</span><br><span class="line">【错误】：参数类型不同，无法拼接!!</span><br></pre></td></tr></table></figure>


<h1 id="Python-中的-init-py"><a href="#Python-中的-init-py" class="headerlink" title="Python 中的 __init__.py"></a>Python 中的 <code>__init__.py</code></h1><p>Python 中的 <code>__init__.py</code> 可以有效地组织一个文件夹下的多个 py 文件，当检测到一个目录下存在<code>__init__.py</code>文件时，就会把它当成一个模块(module)。Module 跟 C＋＋ 的命名空间和 Java 的 Package 的概念相似，都是为了科学地组织化工程，管理命名空间。</p>
<blockquote>
<p><code>__init__.py</code>的设计原则</p>
<ol>
<li>不要污染现有的命名空间。模块一个目的，是为了避免命名冲突，如果你在种用<code>__init__.py</code>时违背这个原则，是反其道而为之，就没有必要使用模块了。</li>
<li>用<code>__init__.py</code>对外提供类型、变量和接口，对用户隐藏各个子模块的实现。一个模块的实现可能非常复杂，你需要用很多个文件，甚至很多子模块来实现，但用户可能只需要知道一个类型和接口。</li>
<li>只在<code>__init__.py</code>中导入有必要的内容，不要做没必要的运算。</li>
</ol>
</blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/115350758" >https://zhuanlan.zhihu.com/p/115350758<i class="fas fa-external-link-alt"></i></a></p>

        </div>

        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Python/">#Python</a>&nbsp;
                    </li>
                
            </ul>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/code_training_2022/2022/06/19/%E7%AC%AC2%E5%91%A8/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">第2周</span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                        </a>
                    </div>
                
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">cm_westwood</a>
        </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#python-magic"><span class="nav-number">1.</span> <span class="nav-text">python magic</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Python-%E4%B8%AD%E7%9A%84%E5%87%BD%E6%95%B0%E9%87%8D%E8%BD%BD"><span class="nav-number">2.</span> <span class="nav-text">Python 中的函数重载</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Python-%E4%B8%AD%E7%9A%84-init-py"><span class="nav-number">3.</span> <span class="nav-text">Python 中的 __init__.py</span></a></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="Search..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/code_training_2022/js/utils.js"></script>

<script src="/code_training_2022/js/main.js"></script>

<script src="/code_training_2022/js/header-shrink.js"></script>

<script src="/code_training_2022/js/back2top.js"></script>

<script src="/code_training_2022/js/dark-light-toggle.js"></script>



    
<script src="/code_training_2022/js/local-search.js"></script>




    
<script src="/code_training_2022/js/code-copy.js"></script>




    
<script src="/code_training_2022/js/lazyload.js"></script>



<div class="post-scripts">
    
        
<script src="/code_training_2022/js/left-side-toggle.js"></script>

<script src="/code_training_2022/js/libs/anime.min.js"></script>

<script src="/code_training_2022/js/toc.js"></script>

    
</div>



</body>
</html>
